package com.mcptt.http.auth.process.digest.result;

import com.mcptt.http.auth.process.result.AuthResult;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;


public class FirstGetResult extends AuthResult
{
    private static final String Err_Key = "error=";
    private String cookie = "";
    private URI location = null;

    private String error = null;
    private String error_description = null;

    public FirstGetResult(int result,String desc)
    {
	super(result,desc);
    }

    public String toString()
    {
	StringBuffer strBuf = new StringBuffer("FirstGetResult  ");
	strBuf.append("cookie=").append(cookie).append(",");
	strBuf.append("location=").append(location).append(";");

	return strBuf.toString();
    }
    public String getErrStr()
    {
	StringBuffer strBuf = new StringBuffer("FirstGetResult  ");
	strBuf.append("result=").append(result);
	if(error != null
		&& error.length() >0)
	{
	    strBuf.append(",");
	    strBuf.append("error=").append(error);
	    if(error_description != null
		    && error_description.length() >0)
	    {
		strBuf.append(",");
		strBuf.append("error_description=").append(error_description).append(";");
	    }
	    else
	    {
		strBuf.append(";");
	    }
	}
	else
	{
	    strBuf.append(";");
	}

	return strBuf.toString();
    }

    public boolean isSuc()
    {
	if(result == 302
		&& (error == null
		|| error.length() <= 0))
	{
	    return true;
	}
	return false;
    }
    
    public boolean checkReturnParamOk()
    {
	if(cookie == null
		|| cookie.length() <= 0
		|| location == null)
	{
	    return false;
	}
	return true;
    }

    public String getCookie()
    {
	return cookie;
    }

    public void setCookie(String cookie)
    {
	this.cookie = cookie;
    }

    public String getLocation()
    {
	if(location != null)
	{
	    if(location.isAbsolute())
	    {
		return location.getPath();
	    }
	    return location.toString();
	}
	return "";
    }

    public void setLocation(String location)
    {
	if(location != null
		&& location.contains(Err_Key))
	{
	    parse(location);
	}
	try
	{
	    this.location = new URI(location);

	} catch (URISyntaxException e)
	{
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }

    private void parse(String authStr)
    {
	if(authStr == null
		|| authStr.length() <= 0)
	{
	    return;
	}
	authStr = authStr.trim();

	int index = authStr.indexOf('?');
	if(index != -1)
	{
	    authStr = authStr.substring(index + 1, authStr.length());
	}

	String[] strArr = authStr.split("&");
	Map<String, String> valueMap = new HashMap<String, String>();
	for (int i = 0; i < strArr.length; i++)
	{
	    String value = strArr[i];
	    String[] valueArr = value.split("=");
	    if (valueArr.length == 2)
	    {
		if (valueArr[0] != null && valueArr[1] != null)
		{
		    String valueStr = valueArr[1].trim();
		    valueMap.put(valueArr[0].trim(), valueStr);
		}
	    }
	}
	error = valueMap.get("error");
	error_description = valueMap.get("error_description");
    }

}
